﻿using System;
using System.Net;
using SDCloudSolution.SDCloud.WebApi.Entities;

namespace SDCloudSolution.SDCloud.WebApi.Exceptions
{
    /// <summary>
    /// Represents an exception of WebApi applications.
    /// </summary>
    public class BaseWebApiException : Exception
    {
        /// <summary>
        /// The results response status code which sets by this exception.
        /// </summary>
        public HttpStatusCode StatusCode { get; private set; }

        /// <summary>
        /// The name of the parameter.
        /// </summary>
        public string ParameterName { get; private set; }

        /// <summary>
        /// The parameter value.
        /// </summary>
        public string ParameterValue { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseWebApiException"/> class.
        /// </summary>
        /// <param name="httpStatusCode">The HTTP status code.</param>
        /// <param name="message">The message.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <param name="parameterValue">The parameter value.</param>
        public BaseWebApiException(HttpStatusCode httpStatusCode, string message, string parameterName, string parameterValue)
            : base(message)
        {
            StatusCode = httpStatusCode;
            ParameterName = parameterName;
            ParameterValue = parameterValue;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseWebApiException"/> class.
        /// </summary>
        /// <param name="httpStatusCode">The HTTP status code.</param>
        /// <param name="message">The message.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        public BaseWebApiException(HttpStatusCode httpStatusCode, string message, string parameterName)
            : this(httpStatusCode, message, parameterName, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseWebApiException"/> class.
        /// </summary>
        /// <param name="httpStatusCode">The HTTP status code.</param>
        /// <param name="message">The message.</param>
        public BaseWebApiException(HttpStatusCode httpStatusCode, string message)
            : this(httpStatusCode, message, null, null)
        {
        }

        /// <summary>
        /// Converts this exception to view model which returned in result response.
        /// </summary>
        /// <returns></returns>
        public virtual ErrorRestModel ToViewModel()
        {
            return string.IsNullOrWhiteSpace(ParameterName)
                ? new ErrorRestModel
                    {
                        Message = Message
                    }
                : new ErrorWithParameterRestModel
                    {
                        Message = Message,
                        ParameterName = ParameterName,
                        ParameterValue = ParameterValue
                    };
        }
    }
}